package com.tusuo.stds.ui.settings;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.text.TextUtils;

import com.hannesdorfmann.mosby.mvp.MvpBasePresenter;
import com.tusuo.stds.MyApp;
import com.tusuo.stds.R;
import com.tusuo.stds.biz.CurrentUser;
import com.tusuo.stds.di.DaggerScope;
import com.tusuo.stds.di.Injector;
import com.tusuo.stds.rest.ApiHelper;
import com.tusuo.stds.rest.ApiResult;
import com.tusuo.stds.rest.AppInfo;
import com.tusuo.stds.ui.about.AboutActivity_;
import com.tusuo.stds.ui.mobilenum.BindActivity_;
import com.tusuo.stds.util.IntentUtils;
import com.tusuo.stds.util.Utils;

import javax.inject.Inject;

import autodagger.AutoComponent;
import autodagger.AutoInjector;
import rx.Subscriber;
import timber.log.Timber;

/**
 * @author J.Huang
 * @version 1.0
 *
 */

@AutoComponent(
        dependencies = MyApp.class
)
@AutoInjector
@DaggerScope(SettingsPresenter.class)
class SettingsPresenter extends MvpBasePresenter<SettingsView> {

    @Inject
    SharedPreferences mSharedPref;
    @Inject
    CurrentUser mCurrentUser;
    @Inject
    ApiHelper mApiHelper;

    private Activity mActivity;
    private String mErrMsgServer;

    private Subscriber<ApiResult> mLogoutSubscriber = null;
    private Subscriber<AppInfo> mAppInfoSubscriber = null;

    SettingsPresenter(Activity activity) {
        mActivity = activity;
        mErrMsgServer = mActivity.getResources().getString(R.string.err_msg_server);


        Injector.initSettingsPresenterComponent().inject(this);
    }

    @Override
    public void detachView(boolean retainInstance) {
        if (mLogoutSubscriber != null) {
            mLogoutSubscriber.unsubscribe();
            mLogoutSubscriber = null;
        }
        if (mAppInfoSubscriber != null) {
            mAppInfoSubscriber.unsubscribe();
            mAppInfoSubscriber = null;
        }
        super.detachView(retainInstance);
    }

    void toMobileNum(String mobileNum) {
        Intent intent = new Intent();
        intent.setClass(mActivity, BindActivity_.class);
        intent.putExtra("mobileNum", mobileNum);
        mActivity.startActivity(intent);
    }

    void checkUpdate() {
        mAppInfoSubscriber = new AppInfoSubscriber();
        mApiHelper.getAppInfo(mAppInfoSubscriber);
    }

    void toAbout() {
        Intent intent = new Intent();
        intent.setClass(mActivity, AboutActivity_.class);
        mActivity.startActivity(intent);
    }

    void logout() {
        mLogoutSubscriber = new LogoutSubscriber();
        mApiHelper.logout(mLogoutSubscriber);
    }

    private class LogoutSubscriber extends Subscriber<ApiResult> {
        @Override
        public void onCompleted() {}

        @Override
        public void onError(Throwable e) {
            Timber.d(e.toString());
            Utils.UI.showToastShort(mErrMsgServer);
        }

        @Override
        public void onNext(final ApiResult result) {
            if (result.checkState(mActivity)) {
                // Logout successfully!
                // 清除本地Cookie
                mCurrentUser.logout();

                IntentUtils.toLogin(mActivity);
            }

        }
    }

    private class AppInfoSubscriber extends Subscriber<AppInfo> {
        @Override
        public void onCompleted() {
            if (!Utils.manualCheckVersion(mActivity, mCurrentUser.getLatestAppVersion(), mCurrentUser.getAppDownloadUrl(), mCurrentUser.hasAppForcedUpdate(), mSharedPref)) {
                if (getView() != null) {
                    getView().showAlreadyNewestVersionTip();
                }
            } else {
                if (getView() != null) {
                    getView().hideVersionTip();
                }
            }
        }

        @Override
        public void onError(Throwable e) {
            Utils.UI.showToastShort(mErrMsgServer);
        }

        @Override
        public void onNext(AppInfo appInfo) {
            if (appInfo.checkState(mActivity)
                    && !TextUtils.isEmpty(appInfo.CookieName)
                    && !TextUtils.isEmpty(appInfo.UserStatus)) {
                mCurrentUser.setAppInfo(appInfo);
            }
        }
    }
}
